package com.lemner.motion.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.telecom.Call;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.base.URL;
import com.lemner.motion.bean.BaseBean;
import com.lemner.motion.util.FullScreen;

public class Register2Activity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
     private Button reg2_btn_done,reg2_btn_code;
     private EditText reg2_et_code,reg2_et_pwd,reg2_et_pwd1,reg2_et_phone;
     private ImageView reg2_iv_back;
     private CheckBox reg2_iv_grass,reg2_iv_grass1;
     private String stunum,phone,schname;
    @Override
    public int getLayoutFile() {
        return R.layout.activity_register2;
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(this,0x33131313);
        reg2_btn_done=(Button)findViewById(R.id.reg2_btn_done);
        reg2_btn_code=(Button)findViewById(R.id.reg2_btn_code);
        reg2_et_code=(EditText)findViewById(R.id.reg2_et_code);
        reg2_et_pwd=(EditText)findViewById(R.id.reg2_et_pwd);
        reg2_et_pwd1=(EditText)findViewById(R.id.reg2_et_pwd1);
        reg2_et_phone=(EditText)findViewById(R.id.reg2_et_phone);
        reg2_iv_back=(ImageView)findViewById(R.id.reg2_iv_back);
        reg2_iv_grass=(CheckBox)findViewById(R.id.reg2_iv_grass);
        reg2_iv_grass1=(CheckBox)findViewById(R.id.reg2_iv_grass1);
    }

    @Override
    public void initEvent() {
        reg2_iv_back.setOnClickListener(this);
        reg2_iv_grass.setOnCheckedChangeListener(this);
        reg2_iv_grass1.setOnCheckedChangeListener(this);
        reg2_btn_code.setOnClickListener(this);
        reg2_btn_done.setOnClickListener(this);
    }

    @Override
    public void initData() {
        schname=getIntent().getStringExtra("schname");
        stunum=getIntent().getStringExtra("stunum");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reg2_iv_back:
                startActivity(new Intent(Register2Activity.this,Register1Activity.class));
                finish();
                break;
            case R.id.reg2_btn_code:
                 phone=reg2_et_phone.getText().toString();
                if(phone.length()!=11)
                    showToast("手机号有误，请检查后再试");
                else {
                    requestCode(phone);
                    countDownTimer.start();
                }
                break;
            case R.id.reg2_btn_done:
           String pwd=reg2_et_pwd.getText().toString();
           String pwd1=reg2_et_pwd1.getText().toString();
           String code=reg2_et_code.getText().toString();
           if (!pwd1.equals(pwd))
               showToast("两次密码不一致，请检查后再试");
           else
               requestReg(stunum,schname,phone,URL.md5(pwd1),code);
                break;
            default:
                break;
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
         switch (compoundButton.getId()){
             case R.id.reg2_iv_grass:
                 if (b) {
                     reg2_iv_grass.setBackground(getDrawable(R.drawable.show));
                     reg2_et_pwd.setTransformationMethod(HideReturnsTransformationMethod
                             .getInstance());
                 } else {
                     reg2_iv_grass.setBackground(getDrawable(R.drawable.hide));
                     reg2_et_pwd.setTransformationMethod(PasswordTransformationMethod
                             .getInstance());
                 }
                 break;
             case R.id.reg2_iv_grass1:
                 if (b) {
                     reg2_iv_grass1.setBackground(getDrawable(R.drawable.show));
                     reg2_et_pwd1.setTransformationMethod(HideReturnsTransformationMethod
                             .getInstance());
                 } else {
                     reg2_iv_grass1.setBackground(getDrawable(R.drawable.hide));
                     reg2_et_pwd1.setTransformationMethod(PasswordTransformationMethod
                             .getInstance());
                 }
                 break;
         }

    }

    final CountDownTimer countDownTimer=new CountDownTimer(60000,1000) {
        @Override
        public void onTick(long l) {
            reg2_btn_code.setClickable(false);
            reg2_btn_code.setTextColor(Color.rgb(182,180,180));
            reg2_btn_code.setText((l/1000+"s")+"后重新获取");
            reg2_btn_code.setBackgroundResource(R.drawable.circle3);
        }

        @Override
        public void onFinish() {
            reg2_btn_code.setText("获取验证码");
            reg2_btn_code.setClickable(true);
            reg2_btn_code.setTextColor(Color.rgb(255,255,255));
            reg2_btn_code.setBackgroundResource(R.drawable.index_circle);
        }
    };


    public void requestReg(String num,String name,String phone,String pwd,String code){
        BaseModel<BaseBean>model=new BaseModel<>();
        retrofit2.Call<BaseBean>call= model.service.getReg(num,name,phone,pwd,code);
        model.callEnqueue(call,listener1);
    }
    public void requestCode(String phone){
        BaseModel<BaseBean>model=new BaseModel<>();
        retrofit2.Call<BaseBean>call=model.service.getCode(phone);
        model.callEnqueue(call,listener);
    }
    BaseListener<BaseBean>listener1=new BaseListener<BaseBean>() {
        @Override
        public void onResponse(BaseBean baseBean) {
            if (baseBean.getErrorCode().equals("0004"))
                showToast("该学号已注册");
            else if (baseBean.getErrorCode().equals("0008"))
                showToast("该手机号已注册");
           else  if(baseBean.getReturnCode().equals("0001")) {
                showToast("注册成功");
                Intent intent = new Intent(Register2Activity.this, LoginActivity.class);
                intent.putExtra("num",phone);
                startActivity(intent);
                finish();
            }

            else
                showToast("注册失败");
        }
        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };
    BaseListener<BaseBean>listener=new BaseListener<BaseBean>() {
        @Override
        public void onResponse(BaseBean baseBean) {
            if(baseBean.getReturnCode().equals("0001"))
                showToast("发送验证码成功");
            else
                showToast("手机号已经使用");
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };


}
